package org.zjt.spiextensiondemo.util;


import org.zjt.spiextensiondemo.annotaion.SPI;
import org.zjt.spiextensiondemo.collector.TraceCollector;



/**
 * @author <a href="juntao.zhang@bkjk.com">juntao.zhang</a>
 * @Description: spi拓展加载工厂
 * @Package org.zjt.spiextensiondemo.util
 * @date 2017/12/4 18:38
 * @see
 */
public class SPIExtensionFactory {

    public <T> T getExtension(Class<T> type)  {
        if (type.isInterface() && type.isAnnotationPresent(SPI.class)) {
            SPIExtensionLoader<T> loader = SPIExtensionLoader.getExtensionLoader(type);
            if (loader.getSupportedExtensions() > 0) {
                return loader.getAdaptiveExtension();
            }
        }
        return null;
    }

    public static void main(String[] args)  {
        TraceCollector extension = new SPIExtensionFactory().getExtension(TraceCollector.class);
        System.err.println(extension.getClass());

        TraceCollector rest = SPIExtensionLoader.getExtensionLoader(TraceCollector.class).getExtension("rest");
        System.err.println(rest.getClass());
    }

}
